package com.dongdongshop.controller;


import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.CartService;
import com.dongdongshop.util.CookieUtils;
import com.dongdongshop.vo.CartVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("cart")
public class CartController {
    @DubboReference(timeout = 100000,check = false)
    private CartService cartService;


    //添加购物车方法
    @RequestMapping("addCart")
    public String addCart(HttpServletRequest request ,HttpServletResponse response,String itemId ,Integer num){
        //1.获取购物车
        Subject subject = SecurityUtils.getSubject();//获取用户
        TbUser user = (TbUser) subject.getPrincipal();//获取用户
        if(user == null){//判段是否登录,如果没有登录就存cookie
            Result<List<CartVo>> result = getCartList(request,response);
            List<CartVo> cartList = result.getData();
            //2.将信息加入购物车
            cartList = cartService.addCartList(cartList,itemId,num);
            String s = JSONUtil.toJsonStr(cartList);
            CookieUtils.setCookie(request,response,"cartList",s,true);
        }else{//如果已经登录就存redis
            Result<List<CartVo>> result = getCartList(request,response);
            List<CartVo> cartList = result.getData();
            //2.将信息加入购物车
            cartList = cartService.addCartList(cartList,itemId,num);
            cartService.addCartListToRedis(cartList,user.getId());
        }
        return "cart";
    }

    //获取购物车方法
    @RequestMapping("getCartList")
    @ResponseBody
    public Result<List<CartVo>> getCartList(HttpServletRequest request,HttpServletResponse response){
        Subject subject = SecurityUtils.getSubject();
        TbUser user = (TbUser) subject.getPrincipal();
        if (user == null){
            List<CartVo> cookieCart = getCookieCart(request);
            return Result.OK().setData(cookieCart);
        }else{
            List<CartVo> redisCart = cartService.getCartListInRedis(user.getId());
            if(redisCart == null){
                String s = JSONUtil.toJsonStr(redisCart);
                s = "[]";
                redisCart = JSONUtil.toList(s, CartVo.class);
            }
            List<CartVo> cookieCart = getCookieCart(request);
            if (cookieCart.size() > 0){
                redisCart = cartService.redisCartAndCookieCart(cookieCart,redisCart);
                CookieUtils.deleteCookie(request,response,"cartList");
            }
            return Result.OK().setData(redisCart);
        }
    }

    @RequestMapping("getCookieCart")
    @ResponseBody
    private List<CartVo> getCookieCart(HttpServletRequest request){
        //获取cookie的值
        String cartList = CookieUtils.getCookieValue(request, "cartList", true);
        if(StrUtil.isBlank(cartList)){//判断是否为空为空赋值[]以免转json失败
            cartList = "[]";
        }
        List<CartVo> cartVos = JSONUtil.toList(cartList, CartVo.class);
        return cartVos;
    }
    @RequestMapping("setCookie")
    @ResponseBody
    private String setCookie(HttpServletRequest request, HttpServletResponse response){
        CookieUtils.setCookie(request,response,"cartList","aaaa",true);
        return "ok";
    }
    @RequestMapping("toCart")
    public String toCart(){
        return "cart";
    }
    @RequestMapping("toOrderInfo")
    public String toOrderInfo(Model m,double price,String[] ids){
        m.addAttribute("price",price);
        m.addAttribute("ids",ids);
        return "getOrderInfo";
    }


}
